package cn.gpnusz.oliverwiki.controller;

import cn.gpnusz.oliverwiki.req.DocSaveReq;
import cn.gpnusz.oliverwiki.resp.DocQueryResp;
import cn.gpnusz.oliverwiki.resp.CommonResp;
import cn.gpnusz.oliverwiki.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author h0ss
 * @description 电子书文档Controller
 * @date 2021/9/6 13:15
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    private DocService docService;

    /**
     * 根据电子书id展示其所有文档数据
     *
     * @param ebookId : 电子书id
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.util.List<cn.gpnusz.oliverwiki.resp.DocQueryResp>>
     * @author h0ss
     */
    @RequestMapping("/list/{ebookId}")
    public CommonResp<List<DocQueryResp>> list(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        resp.setMessage("数据获取成功!");
        resp.setContent(docService.getAll(ebookId));
        return resp;
    }


    /**
     * 新增/编辑电子书文档
     *
     * @param docSaveReq :
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.Object>
     * @author h0ss
     */
    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody DocSaveReq docSaveReq) {
        docService.save(docSaveReq);
        return new CommonResp<>();
    }

    /**
     * 根据id列表删除电子书文档
     *
     * @param ids : id列表
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.Object>
     * @author h0ss
     */
    @DeleteMapping("/delete/{ids}")
    public CommonResp<Object> delete(@PathVariable String ids) {
        docService.delete(ids);
        return new CommonResp<>();
    }

    /**
     * 根据id查询文档内容
     *
     * @param id : 文档id
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.String>
     * @author h0ss
     */
    @RequestMapping("/findContent/{id}")
    public CommonResp<String> findContent(@PathVariable Long id) {
        CommonResp<String> resp = new CommonResp<>();
        resp.setMessage("数据获取成功!");
        resp.setContent(docService.findContent(id));
        return resp;
    }

    /**
     * 文档点赞功能业务接口
     *
     * @param id : 文档id
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.Object>
     * @author h0ss
     */
    @GetMapping("/vote/{id}")
    public CommonResp<Object> vote(@PathVariable Long id) {
        docService.increaseVoteCount(id);
        return new CommonResp<>();
    }

}
